#ifndef __SDL_BUTTON_HPP
#define __SDL_BUTTON_HPP

#include "SDL.h"
#include <string>

#include "sdlcmn.hpp"
#include "sdlevent.hpp"
#include "sdltext.hpp"

#include <iostream>

class sdlButton : public sdlEvent
{
protected:
    SDL_Surface *theScreen;
    std::string  theLabel;
    sdlRect      theRect;

public:
    sdlButton( void * parent = NULL) : sdlEvent(parent)
    {
        theScreen = NULL;
        theLabel  = "";
        theRect.x = -1;
        theRect.y = -1;
        theRect.w = 0;
        theRect.h = 0;
    }

    sdlButton( void * parent,
               SDL_Surface       *parentSurface,
               const SDL_Rect    &aRect,
               const std::string &label = "") : sdlEvent(parent)
    {
        theScreen = parentSurface;
        theRect   = aRect;
        theLabel  = label;
    }

    void update( SDL_Event &e )
    {
        switch( e.type )
        {
          case SDL_MOUSEBUTTONUP:
            sdlRect rect;
            rect.x = e.motion.x;
            rect.y = e.motion.y;
            rect.w = 1;
            rect.h = 1;
            if( theRect.isCollision(rect) )
            {
                callFunction(sdlEVT_COMMAND_BUTTON_CLICKED, e);
            }
            break;
        }
        draw();
        //SDL_Flip( theScreen );
    }

    void clear()
    {
        SDL_FillRect(theScreen, &theRect, SDL_MapRGB(theScreen->format, 3, 100, 180));
    }

    void draw()
    {
        clear();
        sdlTextEngine::getInstance().renderTextToSurface( theScreen, theRect, theLabel.c_str() );
        SDL_UpdateRect( theScreen, theRect.x, theRect.y, theRect.w, theRect.h );
    }
};

#endif //__SDL_BUTTON_HPP
